package com.example.fz338.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;

import com.example.fz338.R;
import com.example.fz338.base.OnFragmentKeyDownListener;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.example.fz338.databinding.ActivityMainBinding;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    // 处理按返回键后的各种情况
    private AppBarConfiguration appBarConfiguration;
    private NavController navController;
    private List<OnFragmentKeyDownListener> onFragmentKeyDownListenerList = new ArrayList<>();
    private long exitTime;
    //
    private ActivityMainBinding binding;

    public void setOnFragmentKeyDownListener(OnFragmentKeyDownListener onFragmentKeyDownListener) {
        onFragmentKeyDownListenerList.add(onFragmentKeyDownListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 生成的就和老师的版本不一样
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_chart, R.id.navigation_video, R.id.navigation_me)
                .build();
        navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(binding.navView, navController);

        //
        NavigationUI.navigateUp(navController, appBarConfiguration);

    }

    /**
     * 返回键(向上导航) 的重写
     */
    @Override
    public boolean onSupportNavigateUp() {
        if(onFragmentKeyDownListenerList.size()!=0) { // 出栈
            onFragmentKeyDownListenerList.remove(onFragmentKeyDownListenerList.size()-1);
        }
        // NullPointerException:navController==null
        return NavigationUI.navigateUp(navController, appBarConfiguration);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 如果键盘按下返回键
        if(event.getAction()==KeyEvent.ACTION_DOWN&&keyCode==KeyEvent.KEYCODE_BACK) {
            if(onFragmentKeyDownListenerList.size()!=0) {// 有二级fragment的情况 - 如APP内的网页跳转
                // 获取栈最上层fragment的情况
                if(onFragmentKeyDownListenerList.get(onFragmentKeyDownListenerList.size()-1)
                        .onKeyDown(keyCode, event)) {
                    return true;
                } else {
                    onSupportNavigateUp();
                }
            } else if (System.currentTimeMillis() - exitTime > 2000) { // 2秒内同时按两次退出才可以退
                Toast.makeText(this, "再次按返回键退出", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                // 假退出 返回桌面
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);
                startActivity(intent);
                // finish(); 是真退出
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}